/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkBaseTemplatedWebControl.
	/// </summary>
	public  abstract class ResLinkBaseTemplatedWebControl: TemplatedWebControl
	{
		static bool _licChecked = false;
		public virtual void CheckLicenseRequirements()
		{
            ResLinkBaseTemplatedWebControl._licChecked = true;
		}

		public ResLinkBaseTemplatedWebControl()
		{
			_currentUser = CSContext.Current.User;
		}
	
		/// <summary>
		/// No Being Span
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
		{
			//we don't need a span tag
		}

		/// <summary>
		/// No End Span
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			//we don't need a span tag
		}

		private User _currentUser = null;

		/// <summary>
		/// The Current User Accessing the blog/data
		/// </summary>
		protected User CurrentUser
		{
			get{return _currentUser;}
		} 

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			CheckLicenseRequirements();
		}


		protected override void OnLoad(EventArgs e)
		{
			CSContext context = CSContext.Current;
			CSContext.Current.SectionID = context.GetIntFromQueryString("SectionID", -1);
			base.OnLoad (e);
		}

		protected override string SkinFolder 
		{
			get 
			{
				return Path.Combine (base.SkinFolder , "ResourceLinks/"); //TODO Use correct skin folder
			}
		}
	
		private ResLinkSection _CurrentSectionData = null;

		public virtual ResLinkSection ThisSection
		{
			get
			{
				if(_CurrentSectionData == null)
				{
					//try and get it from the query string and if not use the current context
					int sectionID = CSContext.Current.GetIntFromQueryString("SectionID" , -1);
					if (sectionID == -1)
					{
						sectionID = CSContext.Current.SectionID;
					}

					_CurrentSectionData =  ResLinkSection.GetSection(sectionID);
					CSContext.Current.SectionID = sectionID;
				}

				return _CurrentSectionData;
			}
			set
			{
				_CurrentSectionData = value;
			}
		}

		protected bool GenericRightsCheckGet(string viewStateID , ResLinkPermissionEnum permission)
		{
			if (ViewState[viewStateID] == null)
				ViewState[viewStateID] = ResLinkPermission.LightAccessCheck(ThisSection, permission , CurrentUser );

			return (bool) ViewState[viewStateID];
		}


		protected bool ModerateRight
		{
			get
			{
				return GenericRightsCheckGet("ModerateRight" ,ResLinkPermissionEnum.Moderate  );
			}
		}

		protected bool AddCommentsRight
		{
			get
			{
				return GenericRightsCheckGet("AddCommentsRight" ,ResLinkPermissionEnum.AddComments  );
			}
		}

		protected bool ViewLinksRight
		{
			get
			{
				return GenericRightsCheckGet("ViewLinksRight" ,ResLinkPermissionEnum.ViewLink  );
			}
		}

		protected bool AddLinksRight
		{
			get
			{
				return GenericRightsCheckGet("AddLinksRight" ,ResLinkPermissionEnum.AddLinks  );
			}
		}

		protected bool EditLinksRight
		{
			get
			{
				return GenericRightsCheckGet("EditLinksRight" ,ResLinkPermissionEnum.EditLinks  );
			}
		}

		protected bool RemoveLinksRight
		{
			get
			{
				return GenericRightsCheckGet("RemoveLinksRight" ,ResLinkPermissionEnum.RemoveLinks );
			}
		}

		protected bool ReportDeadLinksRight
		{
			get
			{
				return GenericRightsCheckGet("ReportDeadLinksRight" ,ResLinkPermissionEnum.ReportDeadLinks );
			}
		}

	}
}
